package com.photoeditor.demo.util;

import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

/**
 * Created by Administrator on 2017/10/27 0027.
 */

public class APKAssertImageLoader {

    private Resources mResources;
    private AsyncTask<String, Void, Bitmap> mImageLoadTask;
    private String mFilePath;

    private APKAssertImageLoader(Resources resources) {
        mResources = resources;
    }

    public static APKAssertImageLoader with(Resources resources) {
        return new APKAssertImageLoader(resources);
    }

    public APKAssertImageLoader load(String filePath) {
        mFilePath = filePath;
        return this;
    }

    public void into(final ImageView imageView) {
        mImageLoadTask = new AsyncTask<String, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(String... params) {
                if (params != null && params.length > 0) {
                    try {
                        String path = params[0];
                        AssetManager assetManager = mResources.getAssets();
                        return BitmapFactory.decodeStream(assetManager.open(path));
                    } catch (Exception ignore) {}
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
        };
        mImageLoadTask.execute(mFilePath);
    }

    public interface OnLoadListener {
        void onLoadSuccess(Bitmap bitmap);

        void onLoadFail();
    }

    public void into(final OnLoadListener listener) {
        mImageLoadTask = new AsyncTask<String, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(String... params) {
                if (params != null && params.length > 0) {
                    try {
                        String path = params[0];
                        AssetManager assetManager = mResources.getAssets();
                        return BitmapFactory.decodeStream(assetManager.open(path));
                    } catch (Exception ignore) {}
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (listener != null) {
                    if (bitmap != null) {
                        listener.onLoadSuccess(bitmap);
                    }else {
                        listener.onLoadFail();
                    }
                }
            }
        };
        mImageLoadTask.execute(mFilePath);
    }

    public Bitmap get() {
        try {
            AssetManager assetManager = mResources.getAssets();
            return BitmapFactory.decodeStream(assetManager.open(mFilePath));
        } catch (Exception ignore) {}
        return null;
    }
}
